package com.untilov.gb.me.gui;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

import com.untilov.gb.URLs;
import com.untilov.gb.http.auth.AuthenticatorIF;
import com.untilov.gb.http.auth.AuthenticatorMEImpl;
import com.untilov.gb.me.GoogleBookmarksMIDlet;

/**
 * Login form.
 * 
 * @author iuntilov
 * 
 */
public class LoginForm extends Form implements CommandListener {

    private TextField login;

    private TextField password;

    private Command exitCommand;

    private Command loginCommand;

    private GoogleBookmarksMIDlet midlet;
    
//    private AuthenticatorIF security;
    
    private Alert connectionIsInProgress;

    public LoginForm(String title, GoogleBookmarksMIDlet midlet) {
        super(title);
        this.midlet = midlet;
        // initial form
        login = new TextField("Login:", "", 35, TextField.ANY);
        password = new TextField("Password:", "", 35, TextField.PASSWORD);
        this.append(login);
        this.append(password);
        this.exitCommand = new Command("Leave", Command.EXIT, 1);
        this.loginCommand = new Command("Login", Command.SCREEN, 1);
        this.addCommand(exitCommand);
        this.addCommand(loginCommand);
        this.setCommandListener(this);
//        this.security = new AuthenticatorMEImpl(URLs.loginURL);
    }

    public Command getExitCommand() {
        return exitCommand;
    }

    public TextField getLogin() {
        return login;
    }

    public Command getLoginCommand() {
        return loginCommand;
    }

    public TextField getPassword() {
        return password;
    }

    /**
     * Command listener.
     */
    public void commandAction(Command c, Displayable d) {
        if (c == getExitCommand()) {
            midlet.destroyApp(false);
            midlet.notifyDestroyed();
        } else if (c == getLoginCommand()) {
            midlet.getDisplay().setCurrent(getConnectionIsInProgressAlert());
            // run gc, and sleep for half a second for gc to complete
            System.gc();
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // do nothing special here, it must comlete success
                e.printStackTrace();
            }
            new Thread(new LoginThread(midlet, 
                                       getLogin().getString(),
                                       getPassword().getString())).start();
        }
    }
    
    /**
     * Returns connection is in progress alert.
     * 
     * @return alert dialog with info about connection progress
     */
    public Alert getConnectionIsInProgressAlert() {
        if (connectionIsInProgress == null) {
            connectionIsInProgress = new Alert("Connecting", 
                    "Data transfer in progress", 
                    null, 
                    AlertType.INFO);
            // timeout for half an hour, but in all cases in will be swithced 
            // by setDisplayble for next, appropriate screen
            connectionIsInProgress.setTimeout(1000 * 60 * 30);
        }
        return connectionIsInProgress;
    }
    
//    /**
//     * Show login failed alert.
//     *
//     */
//    private void showLoginFailed() {
//        this.midlet.getDisplay().setCurrent(midlet.getLoginFailed(""));
//    }
}